﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AutoCode.SqlServer
{
    public partial class MakeController : Form
    {
        public MakeController()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Form load事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MakeController_Load(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// 生成事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            //TODO:上线路径换成 System.Environment.CurrentDirectory 
            string rootPath = new DirectoryInfo("../../").FullName; //程序运行路径
            string codePath = string.Format("{0}\\Code", rootPath); //代码生成目录
            string tlpPath = string.Format("{0}\\{1}", rootPath, txt_tpath.Text); //模板目录
            FileUtils.CreateDirectory(codePath); //1.创建代码生成目录
            DataTable tabs = DBHelper.GetTableBySql(MsSqlCommand.GetTables);//2.获取所有表名
            for (int i = 0; i < tabs.Rows.Count; i++)
            {
                GenerateUtil.MakeSingleTable(codePath, tlpPath, Convert.ToString(tabs.Rows[i][0]),
                    Convert.ToString(tabs.Rows[i]["value"]), "Controller.cs"); //3.生成单文件代码
            }
            lab_res.Text = "操作完成！";
        }

    }
}
